package com.cml.client.domain.entity;

import com.cml.common.dto.entity.ServerNode;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;

/**
 * 姓名：陈茂林
 * 时间：2023/10/16 14:51
 * 描述：
 */
public class Server {
    private String ip;

    private Integer port;

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }


    public String getServerConnDesc() {
        return "{" +
                "ip='" + ip + '\'' +
                ", port=" + port +
                '}';
    }

    public static  class Builder{
        public static Server build(String ip,Integer port){
            Server server = new Server();
            server.setIp(ip);
            server.setPort(port);
            return  server;
        }

        public static  Server build(SocketChannel socket) throws IOException {
            Server server= new Server();
            InetSocketAddress inetSocketAddress = (InetSocketAddress) socket.getRemoteAddress();
            server.setIp(inetSocketAddress.getAddress().getHostAddress());
            server.setPort(inetSocketAddress.getPort());
            return server;
        }

        public static Server build(ServerNode serverNode){
            Server server = new Server();
            server.setIp(serverNode.getIp());
            server.setPort(serverNode.getServiceManagerPort());
            return server;
        }
    }
}
